package com.study;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.study.entity.Person;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class JSONDemo {
    public static void main(String[] args) {

        /*java对象转json格式*/
        List<Person> al = new ArrayList<Person>();
        al.add(new Person(12,"jack",new Date()));
        al.add(new Person(16,"tom",new Date()));
        System.out.println(JSON.toJSONString(al));

        /*JSON 字符串转换为 Java 对象*/
        String jsonStr = "{\"age\":16,\"birth\":1634802480066,\"name\":\"tom\"}";
        Person person = JSON.parseObject(jsonStr,Person.class);
        System.out.println(person);

        String jsonArrStr = "[{\"age\":12,\"birth\":1634803190319,\"name\":\"jack\"},{\"age\":16,\"birth\":1634803190319,\"name\":\"tom\"}]";
        List<Person> personList = JSON.parseArray(jsonArrStr,Person.class);
        System.out.println(personList);

        /*JSON 对象*/
        JSONObject jsonObject = new JSONObject();
        System.out.println(JSONObject.toJSON(al));

    }
}
